// pages/phoneBind/phoneBind.js
const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    password:'',
    codeText:'获取验证码',
    seconds:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (app.globalData.accountInfo.audit_status == 2) {
      this.failureDetails();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      audit_status: app.globalData.accountInfo.audit_status
    })
  },
  //注册失败详情
  failureDetails:function(){
    var that = this;
    api.POST({
      url: app.globalData.base_url + "user/regDetails",
      success: function (res) {
        if (res.data.code == '') {
          that.setData({
            text: res.data.data.reason
          })
          wx.showModal({
            title: '审核失败',
            content: that.data.text,
            cancelText: '知道了',
            confirmText: '重新申请',
            success: function (res) {
              if (res.confirm){
                wx.redirectTo({
                  url: '../register/register'
                });
              }
            }
          })


        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
  //倒计时
  countdown:function(){
    var that =this;
    var timer = setTimeout(function () {
      that.data.seconds--;
      if (that.data.seconds>0){
        that.setData({
          codeText: that.data.seconds+"s"
        })
        that.countdown();
      }else{
        that.setData({
          codeText: "获取验证码"
        })
        clearTimeout(timer);
      }
    }, 1000)
  },
  //获取验证码
  getCode:function(){
    var that = this;
    if (this.data.seconds>0){
      return false
    }
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.phone))) {
      util.showError("手机号不正确");
      return false
    }
    var mobile = that.data.phone
    var type = '23'
    var user_type = '0'
    api.POST({
      params: { user_type: user_type, mobile: mobile, type: type },
      url: app.globalData.base_url + "captcha/get",
      success: function (res) {
        if (res.data.code == '') {
          util.showError("发送成功");
          that.setData({
            seconds: 60,
            codeText: '60s'
          })
          that.countdown();
        } else {
          util.showError(res.data.message);
        }
      }
    })
  },
  login:function(){
    // console.log(this.data.phone)
    if (!(/^1(3|4|5|7|8)\d{9}$/.test(this.data.phone))) {
      util.showError("手机号不正确");
      return false
    }
    if (this.data.password==''){
      util.showError("请输入验证码");
      return false
    }
    var mobile = this.data.phone
    var password = this.data.password
    api.POST({
      params: { captcha: password, mobile: mobile},
      url: app.globalData.base_url + "user/agencyLogin",
      success: function (res) {
        if (res.data.code == '') {
          app.globalData.accountInfo.user_flag=2
          app.globalData.accountInfo.inn_id = res.data.data.inn_id
          util.showError("登录成功");
          wx.switchTab({
            url: '../center/center'
          });
        } else {
          util.showError(res.data.message);
        }
      }
    })

  },
  phoneValue:function(e){
    this.setData({
      phone: e.detail.value
    })
  },
  passwordValue: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  register:function(){
    wx.navigateTo({
      url: '../register/register'
    })
  }
})